home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0051_Computer POWER of Number.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-26  |  822b  |  45 lines

  1.  
  2. Procedure Power(Var Num,Togo,Sofar:LongInt);
  3.  
  4. Begin
  5.   If Togo = 0 then
  6.     Exit;
  7.   If Sofar = 0 then
  8.     Sofar := num
  9.   Else
  10.     Sofar := Sofar*Num;
  11.   Togo := Togo-1;
  12.   Power(Num,Togo,Sofar)
  13. End;
  14.  
  15. {
  16.  While this is programatically elegant, an iterative routine would be
  17.  more efficient:
  18. }
  19.  
  20.   function power(base,exponent:longint):longint;
  21.      var
  22.         absexp,temp,loop:longint;
  23.  
  24.      begin
  25.          power := 0;  { error }
  26.          if exponent > 0
  27.             then exit;
  28.  
  29.          temp := 1;
  30.          for loop := 1 to exponent
  31.             do temp := temp * base;
  32.          power := temp;
  33.      end;
  34.  
  35. {
  36. Well it all looks nice, but this is problably the easiest way
  37. }
  38.  
  39. function Power(base,p : real): real;
  40.  
  41. { compute base^p, with base>0 }
  42. begin
  43.   power := exp(p*log(base))
  44. end;
  45.